/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// ppresent.hpp - PegPresentationManager class defintion.
//
// Author: Kenneth G. Maxwell
//
// Copyright (c) 1997-2000 Swell Software 
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software
// is strictly prohibited.
//
// Notes:
//
// 
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef _PPRESENT_
#define _PPRESENT_



/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
struct Viewport
{
    PegThing *pOwner;
    Viewport *pNext;                // for list management
    PegRect  View;
    PEGBOOL BlockHead;                // for cleanup only,
};


/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
#ifdef PEG_MULTITHREAD

struct PegTaskInfo
{
    PegTaskInfo PEGFAR *pNext;
    PegThing PEGFAR *pThing;

    PEG_TASK_TYPE    pTask;
    PEG_QUEUE_TYPE    pQueue;
};

#endif

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
#if !defined(PEG_KEYBOARD_SUPPORT)
#define ChangeInputThing(a) mpInputThing = a
#endif

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

class PegPresentationManager : public PegWindow
{
    public:
        PegPresentationManager(const PegRect &);
        virtual ~PegPresentationManager();

        virtual void Add(PegThing *What, PEGBOOL DoShow = TRUE);

       #ifdef PEG_MULTITHREAD
        void BeginSubTaskExecute(PegThing *pWin);
       #endif

        virtual void CapturePointer(PegThing *pWho);

       #if defined(PEG_VIEWPORTS)
        void CheckGenerateViewportList(void);
       #endif

        void CheckReduceInvalidSize(PegRect &ParentRect,
            PegRect &ChildRect);

        void ClearScratchPad(void);
        virtual PEGINT DispatchMessage(PegThing *pFrom, PegMessage *pSend);

        virtual void DrawInvalid(void);

       #ifdef PEG_MULTITHREAD
        void EndSubTaskExecute(PegThing *pWin);
       #endif

        virtual PEGINT Execute(PEGBOOL AutoAdd = TRUE);
        PegThing *FindLowestThingContaining(PegThing *pStart, PegPoint Point);

       #if defined(PEG_VIEWPORTS)
        void FreeViewports(PegThing *pStart);

       #ifdef PEG_BUILD_PRESC

        virtual void GenerateViewportList(PegThing *pStart);

       #else

        void GenerateViewportList(PegThing *pStart);

       #endif   // PEG_BUILD_PRESC
       #endif   // PEG_VIEWPORTS

       #ifdef PEG_MULTITHREAD
        PEG_QUEUE_TYPE GetCurrentMessageQueue(void);
       #endif

        PegThing *GetCurrentThing(void) {return mpInputThing;}
        PegThing *GetPointerOwner(void);

       #if defined(PEGWIN32)
        const PEGCHAR *GetScratchPad(void) {return GetWinClipboardData();}
       #else
        const PEGCHAR *GetScratchPad(void) {return mpScratchPad;}
       #endif

        virtual PegScrollDrawInfo *GetVScrollDrawInfo(void)
        {
            return &mVScrollDrawInfo;
        }

        virtual PegScrollDrawInfo *GetHScrollDrawInfo(void)
        {
            return &mHScrollDrawInfo;
        }

        void SetVScrollDrawInfo(PegScrollDrawInfo *pInfo);
        void SetHScrollDrawInfo(PegScrollDrawInfo *pInfo);

        PEGINT VScrollWidth(void) {return mVScrollDrawInfo.Width;}
        PEGINT HScrollHeight(void) {return mHScrollDrawInfo.Width;}

       #ifdef PEG_MULTITHREAD
        PegTaskInfo *GetTaskInfo(void) {return mpTaskInfo;}
        PEG_QUEUE_TYPE GetThingMessageQueue(PegThing* pTarget);
       #endif

        void Invalidate(PegThing *pCaller, const PegRect &Rect);

        PEGBOOL IsPointerCaptured(void);

        PegThing *LastPointerOver(void) {return mpLastPointerOver;}
        void LastPointerOver(PegThing *pOver) {mpLastPointerOver = pOver;}
        virtual PEGINT Message(const PegMessage &Mesg);
        void MoveFocusTree(PegThing *pCurrent);
        virtual void ReleasePointer(PegThing *pWho);
        virtual PegThing *Remove(PegThing *pWhat);

       #ifdef PEG_MULTITHREAD
        PEGBOOL RouteMessageToTask(PegMessage *pMesg);
       #endif

        PegThing *ScreenPop(void);
        PegThing *ScreenPop(PegThing *pWho, PEGBOOL Remove = TRUE,
                            PEGBOOL Destroy = TRUE);
        PEGBOOL ScreenPush(PegThing *pOld, PegThing *pNew,
                           PEGBOOL RemoveOld = TRUE, 
                           PEGBOOL Center = TRUE, PEGBOOL Modal = FALSE);
        void ScreenStackReset(PEGBOOL Remove = FALSE, 
                              PEGBOOL Destroy = TRUE, PEGBOOL Redraw = TRUE);
        void SetExitCallback(void (*pCallback)()) { mpExitCallback = pCallback; }
        void SetScratchPad(const PEGCHAR *pText);
        void SetUserMessageHandler(PEGINT (*pHandler)(const PegMessage &Mesg))
        {
            mpUserMessageHandler = pHandler;
        }

        virtual void ThingDestroyed(PegThing *pCurrent);
        virtual void ThingRemoved(PegThing *pCurrent);

       #if defined(PEG_VIEWPORTS)
        void ViewportChange(PegThing *pChanged);
       #endif

    protected:
        
       #ifdef PEG_VIEWPORTS
        void AddViewport(PegThing *pTarget, PegRect &NewRect);
       #endif

        void AllocateViewportBlock();

       #if defined(PEG_KEYBOARD_SUPPORT)
        void ChangeInputThing(PegThing *pWho);
       #endif

        void ConsolidateInvalidList(void);

        Viewport *GetFreeViewport(void);
        void InsureBranchHasFocus(PegThing *pCurrent);

        virtual void KillFocus(PegThing *pStart);
        virtual void SetFocus(PegThing *pStart);

       #ifdef PEG_VIEWPORTS
        void SplitView(PegThing *pTarget, PegRect Top, PegRect Bottom);
        void SplitView(PegThing *pTarget, PegThing *pChild, PegRect Under);
       #endif
        
       #ifdef PEG_MULTITHREAD
        PegTaskInfo *mpTaskInfo;
       #endif 

        void (*mpExitCallback)();
        PEGINT (*mpUserMessageHandler)(const PegMessage &Mesg);

       #if defined(PEGWIN32) || defined(PEGWINCE)
        void PasteToWindowsClipboard(const PEGCHAR *pText);
        PEGCHAR *GetWinClipboardData(void);
       #endif

        Viewport *mpFreeViewListStart;
        Viewport *mpFreeViewListTail;
        Viewport *mpInvalidList;
        Viewport *mpInvalidListTail;

        PegThing *mpInputThing;
        PegThing *mpDefaultInputThing;
        PegThing *mpInvalidListDrawThing;
        PegThing *mpLastPointerOver;
        PegThing *mpPointerOwners[PEG_MAX_PTR_CAPTURE_NESTING];
        PegThing **mpScreenStack;
        PEGCHAR  *mpScratchPad;
        PegThing *mpViewportChangeStart;
        PegScrollDrawInfo mVScrollDrawInfo;
        PegScrollDrawInfo mHScrollDrawInfo;
        PEGUINT  mTotalViewports;
        PEGINT   mScreenStackIndex;
        PEGBOOL  mMoveFocusFlag;
        PEGUBYTE mPointerCaptures;
};


#endif

